﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using ZG_member.BLL;

namespace ZG_member
{
    public partial class ExpiredInquiry : Form
    {
        public ExpiredInquiry()
        {
            InitializeComponent();
            this.Opacity = 0;
            skinEngine1.SkinFile = Application.StartupPath + ConfigurationManager.AppSettings["Skin"];
        }
        int Num = 1;
        int Size = 12;
        int maxpage = 0;
        string where = "";
        private void timer1_Tick(object sender, EventArgs e)
        {
            this.Left += 2;
            this.Opacity += 0.1;
            if (Opacity == 1)
            {
                this.timer1.Stop();

            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            this.Top = this.Top + 10;
            this.Opacity = this.Opacity - 0.06;
            if (Opacity <= 0)
            {
                this.timer2.Stop();
                this.Close();
            }
        }

        private void ExpiredInquiry_Load(object sender, EventArgs e)
        {
            this.timer1.Start();
            maxpage= dgvDataSource(Size,Num,"");
            lbFenye1.Text = Num.ToString();
            lbFenye2.Text = maxpage.ToString();
            label5.ForeColor = Color.Red;
        }
        int flag = 0;
        private void ExpiredInquiry_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (flag == 0)
            {
                e.Cancel = true;
                flag = 1;
                this.timer2.Start();
            }
        }

        public int dgvDataSource(int PageSize,int PageNum,string where)
        {
            ZG_member.BLL.Leave leave = new ZG_member.BLL.Leave();
            DataTable dt1 = leave.GetList(PageSize, PageNum, where).Tables[0];
            DataTable dt2 = leave.GetListWhere(where).Tables[0];
            double MaxPage = Convert.ToDouble(dt2.Rows.Count) % Convert.ToDouble(Size);
           
            //FFE8A6
            //this.dgvLeave.DataSource = dt1;
            ZG_member.BLL.memberInfo member = new ZG_member.BLL.memberInfo();
            ZG_member.Model.memberInfo memberMode = new ZG_member.Model.memberInfo();
            ZG_member.BLL.consumeSolution consume = new ZG_member.BLL.consumeSolution();
            ZG_member.Model.consumeSolution consumeMode = new ZG_member.Model.consumeSolution();
            //for (int i = 0; i < dt1.Rows.Count; i++)
            //{
            //    string memberID = dt1.Rows[i]["memberId"].ToString();
            //    memberMode = member.GetModels(memberID);
            //    //dgvLeave.Rows[i].Cells["memberName"].Value = memberMode.memberName;
            //    DateTime startTime = Convert.ToDateTime(dt1.Rows[i]["StartDate"]);
            //    DateTime endTime = Convert.ToDateTime(dt1.Rows[i]["EndDate"]);
            //    consumeMode=consume.GetModel(Convert.ToInt32(memberMode.cardType));
            //    int leaveDay =Convert.ToInt32(consumeMode.leaveDate.Substring(0, consumeMode.leaveDate.IndexOf('-')));
            //    int memberLeave = new TimeSpan(endTime.Ticks - startTime.Ticks).Days + 1;
            //    MessageBox.Show(memberLeave.ToString());
            //    if (leaveDay >= memberLeave)
            //    {
                   
            //    }
            //    //if (Convert.ToDateTime(dgvLeave.Rows[i].Cells["EndDate"].Value) <= DateTime.Now)
            //    //{
            //    //    dgvLeave.Rows[i].DefaultCellStyle.BackColor = Color.YellowGreen;
            //    //}
            //}
            this.dgvLeave.DataSource = dt1;
            for (int i = 0; i < dgvLeave.Rows.Count; i++)
            {
                 string memberID = dgvLeave.Rows[i].Cells["memberId"].Value.ToString();
                 memberMode = member.GetModels(memberID);
                 //dgvLeave.Rows[i].Cells["memberName"].Value = memberMode.memberName;
                 DateTime startTime = Convert.ToDateTime(dgvLeave.Rows[i].Cells["StartDate"].Value);
                 DateTime endTime = Convert.ToDateTime(dgvLeave.Rows[i].Cells["EndDate"].Value);
                 consumeMode = consume.GetModel(Convert.ToInt32(memberMode.cardType));
                 int leaveDay = Convert.ToInt32(consumeMode.leaveDate.Substring(0, consumeMode.leaveDate.IndexOf('-')));
                 int memberLeave = new TimeSpan(endTime.Ticks - startTime.Ticks).Days + 1;
                 if (leaveDay <= memberLeave)
                 {
                     dgvLeave.Rows[i].DefaultCellStyle.BackColor = Color.YellowGreen;
                 }
            }
            //for (int i = 0; i < dgvLeave.Rows.Count; i++)
            //{
            //    string memberID = dgvLeave.Rows[i].Cells["memberId"].Value.ToString();
            //    memberMode = member.GetModels(memberID);
            //    dgvLeave.Rows[i].Cells["memberName"].Value = memberMode.memberName;
            //    if (Convert.ToDateTime(dgvLeave.Rows[i].Cells["EndDate"].Value) <= DateTime.Now)
            //    {
            //        dgvLeave.Rows[i].DefaultCellStyle.BackColor = Color.YellowGreen;
            //    }
            //}
            if (MaxPage == 0)
            {
                return Convert.ToInt32(MaxPage);
               
            }
            else
            {
                return (Convert.ToInt32(dt2.Rows.Count) / Convert.ToInt32(Size)) + 1;
            }

        }

        private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (maxpage > Num)
            {
                Num++;
                maxpage = dgvDataSource(Size, Num,where);
                lbFenye1.Text = Num.ToString();
                lbFenye2.Text = maxpage.ToString();
            }
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {

            if (Num>1)
            {
                Num--;
                maxpage = dgvDataSource(Size, Num,where);
                lbFenye1.Text = Num.ToString();
                lbFenye2.Text = maxpage.ToString();
            }
        }

        private void dgvLeave_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            string memberid=  dgvLeave.CurrentRow.Cells["memberId"].Value.ToString();
            ZG_member.BLL.Leave leave = new ZG_member.BLL.Leave();
            ZG_member.Model.Leave leaveMode = new ZG_member.Model.Leave();
            leaveMode=leave.GetModel(memberid);
            LeaveAdd leaveForm = new LeaveAdd(leaveMode);
            leaveForm.ShowDialog();


        }

        private void dgvLeave_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            ZG_member.BLL.memberInfo member = new ZG_member.BLL.memberInfo();
            ZG_member.Model.memberInfo memberMode = new ZG_member.Model.memberInfo();
            for (int i = 0; i < dgvLeave.Rows.Count; i++)
            {
                string memberID = dgvLeave.Rows[i].Cells["memberId"].Value.ToString();
                memberMode = member.GetModels(memberID);
                dgvLeave.Rows[i].Cells["memberName"].Value = memberMode.memberName;
                if (Convert.ToDateTime(dgvLeave.Rows[i].Cells["EndDate"].Value) <= DateTime.Now)
                {
                    dgvLeave.Rows[i].DefaultCellStyle.BackColor = Color.YellowGreen;
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            where = " memberName like '%" + this.txtName.Text + "%' and Leave.memberId like '%" + this.txtMemberID.Text + "%' and StartDate>='" + this.dateTimePicker1.Text + "' and EndDate<='" + this.dateTimePicker2.Text + "'  ";
            maxpage = dgvDataSource(Size, Num, where);
            lbFenye1.Text = Num.ToString();
            lbFenye2.Text = maxpage.ToString();
        }

  

    }
}
